package com.hmyuan.controller.back;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmyuan.domain.Sys_resources;
import com.hmyuan.domain.Sys_role;
import com.hmyuan.domain.Sys_user;
import com.hmyuan.service.SysResourcesService;
/**
 * 菜单controller
 * @author suxiaofei
 *
 */
@Controller
@RequestMapping("/back/sysresources")
public class SysResourcesController {

	@Resource
	SysResourcesService sysResourcesService;
	
	/**
	 * 菜单管理页面
	 */
	@RequestMapping("/sysresourcesPage")
	public String sysresourcesPage(HttpServletRequest request,Model model){
		return "/back/sysresources/sysresourcesPage";
	}
	/**
	 * 菜单新增页面
	 */
	@RequestMapping("/sysresourcesAddPage")
	public String sysresourcesAddPage(HttpServletRequest request,Model model,Sys_resources sys_resources){
		request.setAttribute("sys_resources", sys_resources);
		return "/back/sysresources/sysresourcesAddPage";
	}
	/**
	 * 菜单资源编辑页面
	 */
	@RequestMapping("/sysresourcesUpdatePage")
	public String sysresourcesUpdatePage(HttpServletRequest request,Model model,Long id){
		request.setAttribute("sys_resources", sysResourcesService.selectByPrimaryKey(id));
		return "/back/sysresources/sysresourcesUpdatePage";
	}
	/**
	 * 菜单资源编辑页面提交
	 */
	@ResponseBody
	@RequestMapping("/updatesysresources")
	public String updatesysresources(HttpServletRequest request,Model model,Sys_resources sys_resources){
		sys_resources.setParentid(sys_resources.getParentid()==null?0:sys_resources.getParentid());
		sys_resources.setLevel(sys_resources.getLevel()==null?1:sys_resources.getLevel());
		sysResourcesService.updateByPrimaryKeySelective(sys_resources);
		return "success";
	}
	/**
	 * 新增菜单资源页面提交
	 */
	@ResponseBody
	@RequestMapping("/addsysresources")
	public String addsysresources(HttpServletRequest request,Model model,Sys_resources sys_resources){
		
		sys_resources.setParentid(sys_resources.getParentid()==null?0:sys_resources.getParentid());
		sys_resources.setLevel(sys_resources.getLevel()==null?1:sys_resources.getLevel());
		sys_resources.setIsdelete(0);
		sys_resources.setIshide(0);
		sysResourcesService.insert(sys_resources);
		return "success";
	}
	/**
	 *菜单资源管理页面列表 
	 */
	@RequestMapping("/sysresourcesPageList")
	public String sysresourcesPageList(HttpServletRequest request,Model model){
		
		List<Sys_resources> list = sysResourcesService.selectList(new Sys_resources());
		//将菜单遍历为树形结构
		List<Sys_resources> sysList = new ArrayList<>();
		for (Sys_resources sys : list) {
			for (Sys_resources sys2 : list) {
				if(sys.getParentid()!=null&&sys.getParentid()==sys2.getId()){
					if(sys2.getNodes()==null){sys2.setNodes(new ArrayList<Sys_resources>());}
					sys2.getNodes().add(sys);
				}
			}
			if(null!=sys.getLevel()&&sys.getLevel()==1){//判断是否为主目录
				sysList.add(sys);
			}
		}
		request.setAttribute("sysList", sysList);
		return "/back/sysresources/sysresourcesPageList";
	}
	/**
	 * 检查菜单标识是否可用
	 */
	@ResponseBody
	@RequestMapping("/sysresourcesCheckReskey")
	public String sysresourcesCheckReskey(HttpServletRequest request,Model model,String reskey,Long id){
		if(null==reskey||"".equals(reskey) ) return "fail";
		Sys_resources sys_resources = new Sys_resources();
		sys_resources.setReskey(reskey);
		List<Sys_resources> list = sysResourcesService.selectList(sys_resources);
		if(null==list||list.size()==0||(id!=null&&list.size()==1&&list.get(0).getId()==id)){
			return "success";
		}else{
			return "fail";
		}
	}
	/**
	 * 显示和隐藏菜单资源
	 */
	@ResponseBody
	@RequestMapping("/sysresourcesUpadateishide")
	public String sysresourcesUpadateishide(HttpServletRequest request,Model model,Long[] ids,Integer ishide){
		if(null==ishide||null==ids||ids.length==0) return "fail";
		Sys_resources sys_resources = new Sys_resources();
		sys_resources.setIds(ids);
		sys_resources.setIshide(ishide);//修改状态
		sysResourcesService.updateByPrimaryKeySelective(sys_resources);
		return "success";
	}
	/**
	 * 删除菜单资源
	 */
	@ResponseBody
	@RequestMapping("/sysresourcesDelete")
	public String sysresourcesDelete(HttpServletRequest request,Model model,Long[] ids){
		if(null==ids||ids.length==0) return "fail";
		Sys_resources sys_resources = new Sys_resources();
		sys_resources.setIds(ids);
		sys_resources.setIsdelete(1);
		sysResourcesService.updateByPrimaryKeySelective(sys_resources);
		return "success";
	}
	/**
	 * 菜单资源排序
	 */
	@ResponseBody
	@RequestMapping("/sysresourcesSort")
	public String sysresourcesSort(HttpServletRequest request,Model model,Long[] ids){
		if(null==ids||ids.length==0) return "fail";
		for (int i = 1; i <= ids.length; i++) {
			Sys_resources sys_resources = new Sys_resources();
			sys_resources.setId(ids[i-1]);
			sys_resources.setOrdernum(i);
			sysResourcesService.updateByPrimaryKeySelective(sys_resources);
		}
		return "success";
	}
}
